//
//  StatusDetailTopView.m
//  SinaWeibo
//
//  Created by mj on 13-8-29.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "StatusDetailTopView.h"
#import "AyStatus.h"
#import "AyDefine.h"
#import "NSStringExtend.h"
#import "UIImageExtend.h"

#define kTopHeight 54
#define kBtnTextFont [UIFont systemFontOfSize:13]

@interface StatusDetailTopView()
{
    UIImageView *_bg;
    
    UIButton *_btnRepost;
    UIButton *_btnComment;
    UIButton *_btnFavorite;
    UIImageView *_divider;
    UIImageView *_indivator;
    
    UIButton *_selectedBtn; // 被选中的按钮
}
@end

@implementation StatusDetailTopView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // 1.属性设置
        self.backgroundColor = kGlobalBg;
        // 2.添加子控件
        [self createChildViews];
    }
    return self;
}

#pragma mark 设置微博数据
- (void)setStatus:(AyStatus *)status
{
    _status = status;
    
    // 1.转发
    NSString *repostStr = [[NSString withinWanStr:status.reposts_count] stringByAppendingString:@" 转发"];
    [_btnRepost setTitle:repostStr forState:UIControlStateNormal];
    
    // 2.评论
    NSString *commentStr = [[NSString withinWanStr:status.comments_count] stringByAppendingString:@" 评论"];
    [_btnComment setTitle:commentStr forState:UIControlStateNormal];
    
    // 3.赞
    NSString *favoriteStr = [[NSString withinWanStr:status.attitudes_count] stringByAppendingString:@" 赞"];
    [_btnFavorite setTitle:favoriteStr forState:UIControlStateNormal];
    
    // 4.指示器
    _indivator.center = CGPointMake(_selectedBtn.center.x, _bg.frame.size.height - _indivator.frame.size.height * 0.5);
}

- (void)setFrame:(CGRect)frame
{
    frame.size.width = [UIScreen mainScreen].bounds.size.width;
    frame.size.height = 54;
    [super setFrame:frame];
}

#pragma mark 添加子控件
- (void)createChildViews
{
    CGFloat bgWidth = self.frame.size.width - 2 * kTableBorderPadding;
    CGFloat bgHeight = 44;
    _bg = [[UIImageView alloc] init];
    _bg.frame = CGRectMake(kTableBorderPadding, kTopHeight - bgHeight, bgWidth, bgHeight);
    _bg.userInteractionEnabled = YES;
    _bg.image = [UIImage resizedImageWithName:@"statusdetail_comment_top_background.png"];
    _bg.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    [self addSubview:_bg];
    
    // 0.指示器（三角形）
    _indivator = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"statusdetail_segmented_bottom_arrow.png"]];
    [_bg addSubview:_indivator];
    
    CGFloat btnWidth = 80;
    CGFloat btnHeight = bgHeight;
    // 1.转发
    _btnRepost = [self addFooterBtnWithAction:@selector(btnClick:)];
    _btnRepost.tag = TopViewBtnTypeRepost;
    _btnRepost.frame = CGRectMake(0, 0, btnWidth, btnHeight);
    
    // 2.分隔线
    UIImage *image = [UIImage imageNamed:@"statusdetail_comment_top_rule.png"];
    _divider = [[UIImageView alloc] initWithImage:image];
    _divider.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    _divider.contentMode = UIViewContentModeScaleAspectFit;
    CGFloat dividerX = CGRectGetMaxX(_btnRepost.frame);
    CGFloat dividerY = 0;
    _divider.frame = CGRectMake(dividerX, dividerY, image.size.width, btnHeight);
    [_bg addSubview:_divider];
    
    // 3.评论
    _btnComment = [self addFooterBtnWithAction:@selector(btnClick:)];
    _btnComment.tag = TopViewBtnTypeComment;
    _btnComment.selected = YES;
    _btnComment.frame = CGRectMake(dividerX, 0, btnWidth, btnHeight);
    [self btnClick:_btnComment];
    
    // 4.赞
    _btnFavorite = [self addFooterBtnWithAction:@selector(btnClick:)];
    _btnFavorite.userInteractionEnabled = NO;
    _btnFavorite.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin;
    _btnFavorite.frame = CGRectMake(bgWidth - btnWidth, 0, btnWidth, btnHeight);
    _btnFavorite.tag = TopViewBtnTypeFavorite;
}

#pragma mark 添加footer的按钮
- (UIButton *)addFooterBtnWithAction:(SEL)action
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.titleLabel.font = kBtnTextFont;
    btn.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    [btn setTitleColor:AyColor(56, 56, 56) forState:UIControlStateSelected];
    [btn setTitleColor:AyColor(152, 152, 152) forState:UIControlStateNormal];
    [btn setTitleColor:AyColor(152, 152, 152) forState:UIControlStateHighlighted];
    [btn addTarget:self action:action forControlEvents:UIControlEventTouchDown];
    [_bg addSubview:btn];
    return btn;
}

#pragma mark 转发\评论\赞
- (void)btnClick:(UIButton *)btn
{
    // 1.选中新的按钮
    _selectedBtn.selected = NO;
    btn.selected = YES;
    _selectedBtn = btn;
    
    TopViewBtnType lastType = _type;
    _type = btn.tag;
    
    // 2.更改指示器的位置
    [UIView animateWithDuration:0.3 animations:^{
        CGPoint center = _indivator.center;
        center.x = btn.center.x;
        _indivator.center = center;
    }];
    
    // 3.回调
    if (_topViewBtnClick) {
        _topViewBtnClick(_type, lastType);
    }
    
    
}
@end